package io.renren.modules.carMove.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.carMove.entity.ShootCarMoveEntity;
import io.renren.modules.carMove.entity.ShootCarMoveInfoEntity;
import io.renren.modules.carMove.entity.ShootVehicleEntity;
import io.renren.modules.carMove.service.ShootCarMoveService;
import io.renren.modules.carMove.service.ShootVehicleService;
import io.renren.modules.message.dao.MessageDao;
import io.renren.modules.message.entity.MessageEntity;
import io.renren.modules.picApproval.entity.MediaFileEntity;
import io.renren.modules.picApproval.entity.ShootEntity;
import io.renren.modules.picApproval.service.MediaFileService;
import io.renren.modules.picApproval.service.ShootService;
import io.renren.modules.push.PushService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author wxy
 * @email sharonwang_xy@163.com
 * @date 2018-10-25 16:33:19
 */
@RestController
@RequestMapping("carMove/shootCarMove")
public class ShootCarMoveController {
    @Autowired
    private ShootCarMoveService shootCarMoveService;

    @Autowired
    private ShootService shootService;

    @Autowired
    private ShootVehicleService shootVehicleService;

    @Autowired
    private MediaFileService mediaFileService;
    @Autowired
    private MessageDao messageDao;

    @Autowired
    private PushService pushService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("carMove:shootCarMove:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ShootCarMoveInfoEntity> shootCarMoveInfoEntityList = new ArrayList<>();

        List<ShootEntity> shootList = shootService.queryList(query);
        int total = shootService.queryList(query.noLimit()).size();

        if (params.get("shootType").equals("2")) {  //快速挪车
            for (int i = 0; i < shootList.size(); i++) {
                ShootCarMoveInfoEntity shootCarMoveInfoEntity = new ShootCarMoveInfoEntity();
                ShootEntity shoottemp = shootList.get(i);
                shootCarMoveInfoEntity.setShoot(shoottemp);

                List<ShootVehicleEntity> shootVehicleList = shootVehicleService.queryListByShootId(shoottemp.getShootId());
                if (shootVehicleList.size() > 0) {
                    shootCarMoveInfoEntity.setShootVehicle(shootVehicleList.get(0));
                }
                shootCarMoveInfoEntityList.add(shootCarMoveInfoEntity);
            }
        }
        PageUtils pageUtil = new PageUtils(shootCarMoveInfoEntityList, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 挪车审批
     */
    @RequestMapping("/process")
    @RequiresPermissions("carMove:shootCarMove:process")
    public R carMoveProcess(@RequestBody Map<String, String> params) {

        String shootId = params.get("shootId");
        String processStatus = params.get("processStatus");

        ShootEntity shootCarMoveProcessEntity = new ShootEntity();
        shootCarMoveProcessEntity.setShootId(shootId);
        shootCarMoveProcessEntity.setProcessStatus(processStatus);
        shootCarMoveProcessEntity.setCheckTime(new Date());

        MediaFileEntity mediaProcessEntity = new MediaFileEntity();
        mediaProcessEntity.setShootId(shootId);
        mediaProcessEntity.setProcessStatus(processStatus);

//		System.out.print("shootProcess-------------"+shootProcessEntity.toString());
//		System.out.print("mediaProcess-------------"+mediaProcessEntity.toString());

        shootService.shootProcess(shootCarMoveProcessEntity);
        mediaFileService.mediaProcess(mediaProcessEntity);

        // 新增消息
        MessageEntity messageEntity = new MessageEntity();
        messageEntity.setUsername(shootService.queryObject(shootCarMoveProcessEntity.getShootId()).getUserName());
        messageEntity.setTitle("快速挪车审批结果");
        String result = "处理中";
        if ("1".equals(shootCarMoveProcessEntity.getProcessStatus())) {
            result = "通过";
        }
        if ("2".equals(shootCarMoveProcessEntity.getProcessStatus())) {
            result = "未通过";
        }
        messageEntity.setMessage("您的快速挪车审批结果为：" + result);
        messageEntity.setCreatedate(new Date());
        messageEntity.setType("1");
        messageEntity.setReadstate(0);
        messageDao.save(messageEntity);

        // 发送个推消息
        pushService.pushTransmissionMessageToSingle(messageEntity.getUsername(),messageEntity.getType(), String.valueOf(messageEntity.getId()));

        if (processStatus.equals("1")) { //审批通过给车主发短信

        }

        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{shootId}")
    @RequiresPermissions("carMove:shootCarMove:info")
    public R info(@PathVariable("shootId") String shootId) {
        ShootCarMoveEntity shoot = shootCarMoveService.queryObject(shootId);

        return R.ok().put("shoot", shoot);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("carMove:shootCarMove:save")
    public R save(@RequestBody ShootCarMoveEntity shoot) {
        shootCarMoveService.save(shoot);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("carMove:shootCarMove:update")
    public R update(@RequestBody ShootCarMoveEntity shoot) {
        shootCarMoveService.update(shoot);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("carMove:shootCarMove:delete")
    public R delete(@RequestBody String[] shootIds) {
        shootCarMoveService.deleteBatch(shootIds);

        return R.ok();
    }

}
